package com.volkanic.stages.menu;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.volkanic.buttons.MenuButton;
import com.volkanic.buttons.MotionTextButton;
import com.volkanic.controller.Menu.MenuController;
import com.volkanic.controller.SharedController;
import com.volkanic.stages.SharedStage;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Lucas on 22/07/2014.
 */
public class ButtonStage extends SharedStage {

    /**
     * The list of button of the ButtonStage
     */
    private List<Button> _buttonsList;

    /**
     * Create a new instance of the class ButtonStage
     * @param controller The controller of the game
     */
    public ButtonStage(SharedController controller){
        // set the input processor on this stage
        Gdx.input.setInputProcessor(this);

        // Create the list of buttons
        _buttonsList = new ArrayList<Button>();

        // Create the button, set the InputListener and fill the list of MenuButton
        MotionTextButton play = CreateMenuButton("Play", 0.4f , 16f, 5f);
        play.addListener(new MenuController(controller, SharedController.StageState.WORLD_SELECTION));
        _buttonsList.add(play);

        MotionTextButton highscore = CreateMenuButton("HighScore", 0.45f, 15f, 3.6f);
        highscore.addListener(new MenuController(controller, SharedController.StageState.HIGHSCORE));
        _buttonsList.add(highscore);

        MotionTextButton multi = CreateMenuButton("Multi-player", 0.5f, 14f, 2.4f);
        multi.addListener(new MenuController(controller, SharedController.StageState.MULTIPALYER));
        _buttonsList.add(multi);

        MotionTextButton exit = CreateMenuButton("Exit", 0.55f, 13f, 1.2f);
        exit.addListener(new MenuController(controller, SharedController.StageState.EXIT));
        _buttonsList.add(exit);

        this.addButtonsToStage();
    }

    /**
     * Create a new motion text button
     * @param text the text to write in the button
     * @param speed the speed of the motion
     * @param curY The Y coordinate where the motion start
     * @param endY The Y coordinate where the motion end
     * @return a new motion text button
     */
    private MotionTextButton CreateMenuButton(String text, float speed, float curY, float endY){
        Gdx.app.debug("tag", "Creating button Menu");
        Vector2 origin = new Vector2(X_CENTER, curY * HEIGHT_UNIT);
        Vector2 end = new Vector2(X_CENTER, endY * HEIGHT_UNIT);
        return MenuButton.CreateMenuButton(text, speed * HEIGHT_UNIT, origin, end);
    }

    /**
     * Add each button of the list into the stage
     */
    private void addButtonsToStage() {
        Iterator<Button> iteratorButton = _buttonsList.iterator();
        while(iteratorButton.hasNext()) {
            this.addActor(iteratorButton.next());
        }
    }
}
